import { defineStore } from 'pinia'

export const useTabsStore = defineStore('tabs', {
  state: () => ({
    tabs: [
      { title: '首页', path: '/', name: 'home', closable: false }
    ],
    activeTab: '/'
  }),
  actions: {
    addTab(tab) {
      const exists = this.tabs.some(t => t.path === tab.path)
      if (!exists) {
        this.tabs.push({
          ...tab,
          closable: tab.closable ?? true
        })
      }
      this.activeTab = tab.path
    },
    removeTab(path) {
      if (path === '/') return // 首页不能关闭
      this.tabs = this.tabs.filter(tab => tab.path !== path)
      if (this.activeTab === path) {
        this.activeTab = this.tabs[this.tabs.length - 1]?.path || '/'
      }
    },
    closeOtherTabs(currentPath) {
      this.tabs = this.tabs.filter(tab => !tab.closable || tab.path === currentPath)
    },
    closeAllTabs() {
      this.tabs = this.tabs.filter(tab => !tab.closable)
      this.activeTab = '/'
    }
  }
})